package com.kun.kunkunmall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.kun.common.utils.R;
import com.kun.kunkunmall.ware.feign.MemberFeignService;
import com.kun.kunkunmall.ware.vo.FareRespVo;
import com.kun.kunkunmall.ware.vo.MemberAddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.common.utils.PageUtils;
import com.kun.common.utils.Query;

import com.kun.kunkunmall.ware.dao.WareInfoDao;
import com.kun.kunkunmall.ware.entity.WareInfoEntity;
import com.kun.kunkunmall.ware.service.WareInfoService;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {


    @Autowired
    MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(key)) {
            wrapper.and((w) -> {
                w.eq("id", key)
                        .or().like("name", key)
                        .or().like("address", key)
                        .or().like("areacode", key);
            });
        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                wrapper

        );

        return new PageUtils(page);
    }

    @Override
    public FareRespVo getFare(Long addrId) {
        FareRespVo fareRespVo = new FareRespVo();
        R info = memberFeignService.info(addrId);
        if (info.getCode() == 0) {
            MemberAddressVo memberAddressVo = info.getData("memberReceiveAddress", new TypeReference<MemberAddressVo>() {
            });
            if (memberAddressVo != null) {
                String phone = memberAddressVo.getPhone();
                String substring = phone.substring(phone.length() - 1);
                BigDecimal fare = new BigDecimal(substring);
                fareRespVo.setFare(fare);
                fareRespVo.setAddressVo(memberAddressVo);
                return fareRespVo;
            }
        }
        return null;
    }

}